package com.moon;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;

import java.util.List;
import java.util.Set;

import android.R.color;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sys.DemoApp;
import com.umeng.message.PushAgent;
import com.umeng.update.UmengUpdateAgent;
import com.util.PreferencesUtils;

@SuppressWarnings("deprecation")
public class MainTabActivity extends TabActivity implements
		OnCheckedChangeListener, OnClickListener {
	static public MainTabActivity mMainTabActivity;
	Context mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maintabs);
		mMainTabActivity = this;
		mContext = this;
		
		List<Activity> list = ((DemoApp) this.getApplicationContext())
				.getActivityList();
		int count = list.size();
		for (int i = count - 1; i >= 0; i--) {
			list.get(i).finish();
		}

		((DemoApp) this.getApplicationContext()).mClassesSelected.removeAll(((DemoApp) this.getApplicationContext()).mClassesSelected);
		JSONArray classes = JSON.parseArray(PreferencesUtils.getString(this,
				"classes"));
		if(classes != null) 
		{
			int size = classes.size();
			for (int i = 0; i < size; i++) {
				JSONObject item = classes.getJSONObject(i);
				((DemoApp) this.getApplicationContext()).mClassesSelected.add(item.getInteger("id"));
			}
		}

		setupIntent();
		((RadioButton) findViewById(R.id.radio_button0)).setChecked(true);
		((RadioButton) findViewById(R.id.radio_button0)).setTextColor(Color
				.parseColor("#3b5997"));

		PushAgent mPushAgent = PushAgent.getInstance(this);
		mPushAgent.enable();
		
		UmengUpdateAgent.update(this);
		UmengUpdateAgent.setUpdateOnlyWifi(false);

		findViewById(R.id.radio_button0).setOnClickListener(this);
		findViewById(R.id.radio_button1).setOnClickListener(this);
		findViewById(R.id.radio_button2).setOnClickListener(this);
		findViewById(R.id.radio_button3).setOnClickListener(this);

		setChatMsg();
	}
	
	public void onResume() {
		super.onResume();
	}

	public void onPause() {
		super.onPause();
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {

			((RadioButton) findViewById(R.id.radio_button0)).setTextColor(Color
					.parseColor("#a7a7a7"));
			((RadioButton) findViewById(R.id.radio_button1)).setTextColor(Color
					.parseColor("#a7a7a7"));
			((RadioButton) findViewById(R.id.radio_button2)).setTextColor(Color
					.parseColor("#a7a7a7"));
			((RadioButton) findViewById(R.id.radio_button3)).setTextColor(Color
					.parseColor("#a7a7a7"));

			switch (buttonView.getId()) {
			case R.id.radio_button0:
				this.getTabHost().setCurrentTabByTag("A_TAB");
				((RadioButton) findViewById(R.id.radio_button0))
						.setChecked(true);
				((RadioButton) findViewById(R.id.radio_button0))
						.setTextColor(Color.parseColor("#3b5997"));
				break;
			case R.id.radio_button1:
				this.getTabHost().setCurrentTabByTag("B_TAB");
				((RadioButton) findViewById(R.id.radio_button1))
						.setChecked(true);
				((RadioButton) findViewById(R.id.radio_button1))
						.setTextColor(Color.parseColor("#3b5997"));
				break;
			case R.id.radio_button2:
				this.getTabHost().setCurrentTabByTag("C_TAB");
				((RadioButton) findViewById(R.id.radio_button2))
						.setChecked(true);
				((RadioButton) findViewById(R.id.radio_button2))
						.setTextColor(Color.parseColor("#3b5997"));
				break;
			case R.id.radio_button3:
				this.getTabHost().setCurrentTabByTag("D_TAB");
				((RadioButton) findViewById(R.id.radio_button3))
						.setChecked(true);
				((RadioButton) findViewById(R.id.radio_button3))
						.setTextColor(Color.parseColor("#3b5997"));
				break;
			}
		}
	}

	private void setupIntent() {
		getTabHost().addTab(
				buildTabSpec("A_TAB", R.string.app_name, R.drawable.btn_tab0,
						new Intent(this, MainPageActivity.class)));
		getTabHost().addTab(
				buildTabSpec("B_TAB", R.string.app_name, R.drawable.btn_tab1,
						new Intent(this, InteractActivity.class)));
		getTabHost().addTab(
				buildTabSpec("C_TAB", R.string.app_name, R.drawable.btn_tab2,
						new Intent(this, AppActivity.class)));
		getTabHost().addTab(
				buildTabSpec("D_TAB", R.string.app_name, R.drawable.btn_tab3,
						new Intent(this, MineActivity.class)));
	}

	public void setCurrentTab(int index) {
		getTabHost().setCurrentTab(index);

		((RadioButton) findViewById(R.id.radio_button0))
				.setBackgroundColor(color.transparent);
		((RadioButton) findViewById(R.id.radio_button1))
				.setBackgroundColor(color.transparent);
		((RadioButton) findViewById(R.id.radio_button2))
				.setBackgroundColor(color.transparent);
		((RadioButton) findViewById(R.id.radio_button3))
				.setBackgroundColor(color.transparent);

		((RadioButton) findViewById(R.id.radio_button0 + index))
				.setChecked(true);
		((RadioButton) findViewById(R.id.radio_button0 + index))
				.setBackgroundColor(Color.parseColor("#4762a8"));
	}

	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
			final Intent content) {
		return getTabHost()
				.newTabSpec(tag)
				.setIndicator(getString(resLabel),
						getResources().getDrawable(resIcon))
				.setContent(content);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {// 捕捉返回键
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exitApp();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private long exitTime = 0;

	public void exitApp() {
		if ((System.currentTimeMillis() - exitTime) > 2000) {
			Toast.makeText(this, "再按一次退出 应用", Toast.LENGTH_SHORT).show();
			exitTime = System.currentTimeMillis();
		} else {
			finish();
		}
	}

	public void setNewMsg(int firstNum, boolean firstRedDot) {
		findViewById(R.id.new0).setVisibility(View.GONE);
		findViewById(R.id.new1).setVisibility(View.GONE);

		if (firstNum > 0) 
		{
			findViewById(R.id.new0).setVisibility(View.VISIBLE);
			((TextView) findViewById(R.id.new0)).setText(String.valueOf(firstNum));
		}
		else if(firstRedDot)
		{
			findViewById(R.id.new0).setVisibility(View.VISIBLE);
			((TextView) findViewById(R.id.new0)).setText("");
		}
	}

	Handler handle = new Handler() {
		public void handleMessage(android.os.Message msg) {
			try {
				int num = RongIM.getInstance().getRongIMClient()
						.getTotalUnreadCount();
				if (num > 0) {
					findViewById(R.id.new2).setVisibility(View.VISIBLE);
					((TextView) findViewById(R.id.new2)).setText(String
							.valueOf(num));
				} else {
					findViewById(R.id.new2).setVisibility(View.GONE);
				}
				
				MainPageActivity.mMainPageActivity.setIconNumber();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			super.handleMessage(msg);
		}
	};

	public void setChatMsg() {
		handle.sendEmptyMessage(0);
	}

	@Override
	public void onClick(View arg0) {
		((RadioButton) findViewById(R.id.radio_button0)).setTextColor(Color
				.parseColor("#a7a7a7"));
		((RadioButton) findViewById(R.id.radio_button1)).setTextColor(Color
				.parseColor("#a7a7a7"));
		((RadioButton) findViewById(R.id.radio_button2)).setTextColor(Color
				.parseColor("#a7a7a7"));
		((RadioButton) findViewById(R.id.radio_button3)).setTextColor(Color
				.parseColor("#a7a7a7"));

		((RadioButton) findViewById(R.id.radio_button0)).setChecked(false);
		((RadioButton) findViewById(R.id.radio_button1)).setChecked(false);
		((RadioButton) findViewById(R.id.radio_button2)).setChecked(false);
		((RadioButton) findViewById(R.id.radio_button3)).setChecked(false);

		switch (arg0.getId()) {
		case R.id.radio_button0:
			this.getTabHost().setCurrentTabByTag("A_TAB");
			((RadioButton) findViewById(R.id.radio_button0)).setChecked(true);
			((RadioButton) findViewById(R.id.radio_button0)).setTextColor(Color
					.parseColor("#3b5997"));
			break;
		case R.id.radio_button1:
			this.getTabHost().setCurrentTabByTag("B_TAB");
			((RadioButton) findViewById(R.id.radio_button1)).setChecked(true);
			((RadioButton) findViewById(R.id.radio_button1)).setTextColor(Color
					.parseColor("#3b5997"));
			break;
		case R.id.radio_button2:
			this.getTabHost().setCurrentTabByTag("C_TAB");
			((RadioButton) findViewById(R.id.radio_button2)).setChecked(true);
			((RadioButton) findViewById(R.id.radio_button2)).setTextColor(Color
					.parseColor("#3b5997"));
			break;
		case R.id.radio_button3:
			this.getTabHost().setCurrentTabByTag("D_TAB");
			((RadioButton) findViewById(R.id.radio_button3)).setChecked(true);
			((RadioButton) findViewById(R.id.radio_button3)).setTextColor(Color
					.parseColor("#3b5997"));
			break;
		}
	}
	
	@Override  
	public Resources getResources() {  
	    Resources res = super.getResources();    
	    Configuration config=new Configuration();    
	    config.setToDefaults();    
	    res.updateConfiguration(config,res.getDisplayMetrics() );  
	    return res;  
	}  
}
